//
//  SQLiteFs.cpp
//  FuseSQLite
//
//  Created by Admin on 7/14/14.
//  Copyright (c) 2014 Admin. All rights reserved.
//

#include "SQLiteFs.h"

#include <errno.h>
#include <exception>

SQLiteFs::SQLiteFs(const char* dbPath)
: m_sqliteFsRepo(dbPath)
{
    m_sqliteFsRepo.createHardLinksTable();
    m_sqliteFsRepo.addFolderLink("/", 0, 0777 | S_IFDIR, 0);
}

int SQLiteFs::getAttributes(const char *path, struct stat* stbuf)
{
    try
    {
        HardLink hardLink = m_sqliteFsRepo.getHardLinkInfo(0);
        
        stbuf->st_size = 0;
        stbuf->st_flags = hardLink.attributes;
        stbuf->st_nlink = 0;
        
        switch (hardLink.nodeType)
        {
            case HardLink::kFolder:
                stbuf->st_flags |= S_IFDIR;
                break;
                
            default:
                break;
        }
        
    }
    catch (const std::exception& e)
    {
        return -EACCES;
    }
    
    return 0;
}

int SQLiteFs::createDirectory(const char* path, mode_t mode)
{
    return -EACCES;
}

int SQLiteFs::removeDirectory(const char* path)
{
    return -EACCES;
}

int SQLiteFs::readDirectory(const char *path, void *buf, fuse_fill_dir_t filler,
                  off_t offset, struct fuse_file_info *fi)
{
    return -EACCES;
}

int SQLiteFs::rename(const char* path, const char* newPath)
{
    return -EACCES;
}
